/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Game/Server/GameSession.h"
#include <memory>
#include "Shared/DB/DatabaseImpl.h"
#include "Shared/Log/Log.h"
#include "Game/Global.h"
#include "Game/Game/Pet.h"
#include "Game/Game/Player.h"
#include "Game/World/World.h"
using namespace Shared;
using namespace Shared::DB;
using namespace Shared::Binary;


class CharacterHandler
{
public:
    void handleCharEnumCallback(QueryResult* result, std::uint32_t account)
    {
        std::auto_ptr<QueryResult> resultPtr(result);
        GameSession* session = gameWorld.findSession(account);
        if (!session) {
            return;
        }
        session->handleCharEnum(result);
    }
} chrHandler;

void GameSession::handleCharEnumOpcode(Shared::Binary::WorldPacket &packet)
{
    sharedLog.outDebug("enter GameSession::handleCharEnumOpcode()");

    gameSystem.charaDB().asyncPQuery(&chrHandler, &CharacterHandler::handleCharEnumCallback, accountId(),
        //           0               1                2                3                 4                  5                       6                        7
        "SELECT characters.guid, characters.name, characters.race, characters.class, characters.gender, characters.playerBytes, characters.playerBytes2, characters.level, "
        //   8                9               10                     11                     12                     13                    14
        "characters.zone, characters.map, characters.position_x, characters.position_y, characters.position_z, guild_member.guildid, characters.playerFlags, "
        //  15                    16                   17                     18                   19
        "characters.at_login, character_pet.entry, character_pet.modelid, character_pet.level, characters.equipmentCache "
        "FROM characters LEFT JOIN character_pet ON characters.guid=character_pet.owner AND character_pet.slot='%u' "
        "LEFT JOIN guild_member ON characters.guid = guild_member.guid "
        "WHERE characters.account = '%u' ORDER BY characters.guid",
        PET_SAVE_AS_CURRENT, accountId());
}

void GameSession::handleCharEnum(Shared::DB::QueryResult *rs)
{
    WorldPacket data(SMSG_CHAR_ENUM, 100);
    std::uint8_t num = 0;

    data << num;
    if (rs)
    {
        do
        {
            std::uint32_t guidlow = (*rs)[0].toUInt32();
            sharedLog.outDebug("build enum data for char guid %u from account %u.", guidlow, accountId());
            if (Player::buildCharaEnumData(rs, data))
            { ++num; }
        }
        while (rs->nextRow());
    }

    data.put<std::uint8_t>(0, num);
    sendPacket(data);
}

void GameSession::handleCharCreateOpcode(Shared::Binary::WorldPacket &packet)
{

}